package com.managerplat.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.managerplat.dao.SellerDAO;
import com.managerplat.model.DishInfo;
import com.managerplat.model.SellerInfo;
import com.managerplat.model.Sellers;

@Repository("sd")
@Transactional
public class SellerDAOImpl implements SellerDAO {
	@PersistenceContext(name="un")
	private EntityManager em;
	private ImageChange imageChange = new ImageChange();
	@Override
	public List<SellerInfo> findSellerList() {
		// TODO Auto-generated method stub
		String jpql="select s.sellerId,s.sellerName,s.sellerImagePath,s.sellerPhone,s.sellerStar from Sellers s where s.isRegist=1";
		List<Object> ls=(List<Object>)em.createQuery(jpql)
				.getResultList();
		List<SellerInfo> list = new ArrayList<SellerInfo>();
		Iterator<Object> iterator = ls.iterator();
		while(iterator.hasNext()){
			//[1, xx, E:\SellerImage\seller1.tmp, 1300638857, 5.0]
			//Sellers obj = (Sellers) iterator.next();
			Object[] obj = (Object[]) iterator.next();
			String sellerImagePath = String.valueOf(obj[2]);
			
			SellerInfo sellerInfo = new SellerInfo();
			sellerInfo.setSellerId(Integer.parseInt(String.valueOf(obj[0])));
			sellerInfo.setSellerName(String.valueOf(obj[1]));
			sellerInfo.setSellerImage(imageChange.imageToByte(sellerImagePath));
			sellerInfo.setSellerStar(Double.parseDouble(String.valueOf(obj[4])));
			sellerInfo.setSellerPhone(Integer.parseInt(String.valueOf(obj[3])));
			list.add(sellerInfo);	
		}
		return list;
	}
	public List<DishInfo> findSellerMenu(int sellerId) {
		String jpql="select d.dishId,d.dishName,d.dishPrice,d.dishImagePath from DishInfo d where d.sellerId=:sellerId";
		List<Object> ls=(List<Object>)em.createQuery(jpql)
				.setParameter("sellerId", sellerId)
				.getResultList();
		List<DishInfo> list = new ArrayList<DishInfo>();
		Iterator<Object> iterator = ls.iterator();
		while(iterator.hasNext()){
			Object[] obj = (Object[]) iterator.next();
			String dishImagePath = String.valueOf(obj[3]);
			//[1, banfan, 12.0, E:\sellerMenu_image\banfan.jpg]
			DishInfo dish = new DishInfo();
			dish.setDishImage(imageChange.imageToByte(dishImagePath));
			dish.setDishId(Integer.parseInt(String.valueOf(obj[0])));
			dish.setDishName(String.valueOf(obj[1]));
			dish.setDishPrice(Double.parseDouble(String.valueOf(obj[2])));
			list.add(dish);
		}
		return list;
	}
	@Override
	public int saveSellersRegistInfo(Sellers seller) {
		// TODO Auto-generated method stub
		seller.setIsRegist(0);
		em.persist(seller);
		System.out.println(seller.getSellerId());
		return seller.getSellerId();
	}
	@Override
	public List<Sellers> findUnRegistSellers(int isRegist) {
		// TODO Auto-generated method stub
		String jpql = "select s from Sellers s where s.isRegist=:isRegist";
		List<Sellers> list = em.createQuery(jpql).setParameter("isRegist", isRegist).getResultList();
//		for(Sellers s:list)
//		{
//			System.out.println(s.getSellerName());
//		}
		return list;
	}
	@Override
	public int checkSeller(String sellerName, String sellerPassword) {
		// TODO Auto-generated method stub
		String jpql = "select s from Sellers s where s.sellerName=:sellerName and s.sellerPassword=:sellerPassword";
		List<Sellers> list = em.createQuery(jpql)
				.setParameter("sellerName", sellerName)
				.setParameter("sellerPassword", sellerPassword)
				.getResultList();
		int id = 0;
		for(Sellers s:list)
		{
			System.out.println(s.getSellerId());
			id = s.getSellerId();
		}
		return id;
	}
	@Override
	public int saveMenu(List<DishInfo> listDishInfo) {
		// TODO Auto-generated method stub
		for(DishInfo dish : listDishInfo)
		{
			//
		}
		return 0;
	}
	@Override
	public void updateIsRegist(int sellerId) {
		// TODO Auto-generated method stub
		String jpql = "update Sellers s set s.isRegist=1 where s.sellerId=:sellerId";
		Query query = em.createQuery(jpql).setParameter("sellerId", sellerId);
		query.executeUpdate();
	}
}






